package br.ucb.tcc.helper;

import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.CompactWriter;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class AimlHelper {
	
	private static String[] toNormalize = {
			"\n",
			"<value>",
			"</value>",
			"<command>",
			"</command>"			
			};
	
	public static String normalizeExpression(String aiml){
		String xmlNormalize = aiml;
		for (String toReplace : toNormalize) {
			xmlNormalize = xmlNormalize.replace(toReplace, "");
		}
		return xmlNormalize.trim();
	}
	
	public static String toAiml(Object command){
		XStream xstream = new XStream(new DomDriver("ISO-8859-1"));
		xstream.autodetectAnnotations(true);		
		ByteOutputStream stream = new ByteOutputStream();
		try {
			xstream.marshal(command, new CompactWriter(new OutputStreamWriter(stream, "ISO-8859-1")));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}		
		return stream.toString();
	}
	
	public static String magicConversion(Object command) {
		return normalizeExpression(toAiml(command));	
	}
}
